package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.BaseException;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    @GetMapping("/saveData/吴思远/18/男")
    public AjaxResult save1(String name,Integer age,String sex){
        boolean save1 = reviewService.save1(name, age, sex);
        if (!save1){
            throw new BaseException("插入失败");
        }
        return new AjaxResult(200,"成功插入:1条数据");

    }

    @PostMapping("/saveData")
    public AjaxResult save2(@RequestBody Review review){
        boolean save2 = reviewService.save2(review);
        if (!save2){
            throw new BaseException("插入失败");
        }
        return new AjaxResult(200,"成功插入:1条数据");
    }
    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}")
    public AjaxResult delete(@PathVariable int id){
        boolean delete = reviewService.delete(id);
        if (!delete){
            throw new BaseException("删除失败");
        }
        return new AjaxResult(200,"成功删除:1条数据");
    }
    /**=========================================================修改数据=============================================*/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        boolean update = reviewService.update(review);
        if (!update){
            throw new BaseException("修改失败");
        }
        return new AjaxResult(200,"修改成功");
    }

    /**=========================================================查询数据=============================================*/
    @GetMapping("/getById")
    public Review getById(Long id){
        Review review = reviewService.getById(id);
        if (review == null){
            throw new BaseException("查询失败");
        }
        return review;
    }


    @GetMapping("/getDataByPage")
    public TableDataInfo getByPage(){
        startPage();
        List<Review> service = reviewService.getByPage1();
        if (service == null){
            throw new BaseException("查询失败");
        }
        return getDataTablePage(service);
    }
}